package com.yipingnai.blog.controller.admin;

import com.yipingnai.blog.model.Category;
import com.yipingnai.blog.model.RespBean;
import com.yipingnai.blog.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: jiangym
 * @Date: 2020/5/19 10:06
 */
@RestController
@RequestMapping("/admin/category")
public class AdminCategoryController {

    @Autowired
    CategoryService categoryService;
    @GetMapping("/")
    public List<Category> getCategories(){
        return categoryService.getAllCategories();
    }

    @PostMapping("/add")
    public RespBean addCategory(@RequestBody Category category){
        if(categoryService.addCategory(category)){
            return RespBean.ok("添加成功");
        }
        return RespBean.error("添加失败");
    }

    @DeleteMapping("/")
    public RespBean deleteCategory(Integer id){
        if(categoryService.deleteCategory(id)){
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @PutMapping("/")
    public RespBean updateCategoryIsUse(Integer id,Integer isUse){
        if(categoryService.updateCategoryIsUse(id,isUse) > 0){
            return RespBean.ok("修改成功");
        }
        return RespBean.error("修改失败");

    }
}
